<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/9/19
 * Time: 11:01
 */

namespace app\admin\controller;

use think\Db;

class Bigintegral extends Base
{
    /**
     * 家大业大积分管理首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $binte_number = trim(input('binte_number'), ' '); // 流水号
            $user_real_name = trim(input('user_real_name'), ' '); // 用户名
            $user_phone = trim(input('user_phone'), ' '); // 手机号
            $binte_desc = trim(input('binte_desc'));
            $start_time = input('start_time');
            $end_time = input('end_time');
            $binte_type = input('binte_type'); // 1 收入  2 支出
            $binte_freeze = input('binte_freeze'); // 1 启用   2 冻结

            $where = [];
            if(isset($binte_number) && $binte_number != ''){
                $where['binte_number'] = ['like', '%' . $binte_number . '%'];
            }
            if(isset($user_real_name) && $user_real_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($binte_desc) && $binte_desc != ''){
                $where['binte_desc'] = ['like', '%' . $binte_desc . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['binte_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['binte_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['binte_time'] = ['<=', $end_time];
            }
            if(isset($binte_type) && $binte_type != ''){
                $where['binte_type'] = $binte_type;
            }
            if(isset($binte_freeze) && $binte_freeze != ''){
                $where['binte_freeze'] = $binte_freeze;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_big_integral')
                ->join('up_u_user', 'binte_user_id = user_id', 'LEFT')
                ->where($where)
                ->count('binte_id');
            $lists = Db::table('up_big_integral')
                ->join('up_u_user', 'binte_user_id = user_id', 'LEFT')
                ->field('binte_id, binte_type, binte_number, binte_desc, binte_price, binte_front, 
                binte_after, binte_time, user_real_name, user_phone, binte_admin_id, binte_status, binte_freeze')
                ->where($where)
                ->order('binte_time desc, binte_id desc')
                ->page($Nowpage, $limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 显示/隐藏
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function changeStatus(){
        $binte_id = input('id');
        $status = Db::table('up_big_integral')->where('binte_id', $binte_id)->field('binte_status')->find();
        $newStatus = $status['binte_status'] == 1 ? 2 : 1;
        $res = Db::table('up_big_integral')->where('binte_id', $binte_id)->update(['binte_status' => $newStatus]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 积分冻结/启用
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function setFreeze(){
        $binte_id = input('id');
        $freeze = Db::table('up_big_integral')
            ->join('up_u_user', 'user_id = binte_user_id', 'LEFT')
            ->where('binte_id', $binte_id)
            ->field('binte_freeze, binte_user_id, user_big_balance, binte_price')
            ->find();
        Db::startTrans();
        try{
            if($freeze['binte_freeze'] == 1){
                if($freeze['user_big_balance'] < $freeze['binte_price']){
                    return json(['code' => 100, 'msg' => '冻结积分大于可用积分']);
                }
                Db::table('up_u_user')
                    ->where('user_id', $freeze['binte_user_id'])
                    ->setDec('user_big_balance', $freeze['binte_price']);
                $newFreeze = 2;
            }else{
                Db::table('up_u_user')
                    ->where('user_id', $freeze['binte_user_id'])
                    ->setInc('user_big_balance', $freeze['binte_price']);
                $newFreeze = 1;
            }
            Db::table('up_big_integral')->where('binte_id', $binte_id)->setField('binte_freeze', $newFreeze);

            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch (\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $inte_ids = input('ids');
        $where = [];

        if($inte_ids == -1){
            $binte_number = trim(input('binte_number'), ' '); // 流水号
            $user_real_name = trim(input('user_real_name'), ' '); // 用户名
            $user_phone = trim(input('user_phone'), ' '); // 手机号
            $binte_desc = trim(input('binte_desc'));
            $start_time = input('start_time');
            $end_time = input('end_time');
            $binte_type = input('binte_type'); // 1 收入  2 支出
            $binte_freeze = input('binte_freeze'); // 1 启用   2 冻结

            if(isset($binte_number) && $binte_number != ''){
                $where['binte_number'] = ['like', '%' . $binte_number . '%'];
            }
            if(isset($user_real_name) && $user_real_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($binte_desc) && $binte_desc != ''){
                $where['binte_desc'] = ['like', '%' . $binte_desc . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['binte_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['binte_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['binte_time'] = ['<=', $end_time];
            }
            if(isset($binte_type) && $binte_type != ''){
                $where['binte_type'] = $binte_type;
            }
            if(isset($binte_freeze) && $binte_freeze != ''){
                $where['binte_freeze'] = $binte_freeze;
            }
        }else{
            if($inte_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            $where['binte_id'] = ['IN', $inte_ids];
        }

        $lists = Db::table('up_big_integral')
            ->join('up_u_user', 'binte_user_id = user_id', 'LEFT')
            ->field('binte_id, binte_type, binte_number, binte_desc, binte_price, binte_front, binte_after, 
                binte_time, user_real_name, user_phone')
            ->where($where)
            ->order('binte_time desc')
            ->select();
        foreach($lists as $k => $v){
            // 类型
            if($v['binte_type'] == 1){
                $lists[$k]['binte_type'] = '收入';
            }elseif($v['binte_type'] == 2){
                $lists[$k]['binte_type'] = '支出';
            }
            // 积分值
            $lists[$k]['binte_price'] = "\t" . $v['binte_price'] . "\t";
            // 变动前积分
            $lists[$k]['binte_front'] = "\t" . $v['binte_front'] . "\t";
            // 变动后积分
            $lists[$k]['binte_after'] = "\t" . $v['binte_after'] . "\t";
            // 手机号
            $lists[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
        }
        $cellname = [
            ['user_real_name', '用户', 10, 'LEFT'],
            ['user_phone', '用户手机号', 20, 'LEFT'],
            ['binte_type', '类型', 10, 'LEFT'],
            ['binte_desc', '描述', 20, 'LEFT'],
            ['binte_number', '流水号', 25, 'LEFT'],
            ['binte_front', '变动前积分', 20, 'LEFT'],
            ['binte_price', '积分值', 15, 'LEFT'],
            ['binte_after', '变动后积分', 20, 'LEFT'],
            ['binte_time', '记录时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('家大业大积分明细', 'Bo_integral', $cellname, $lists);
        return json($res);
    }
}